package AppliactionOfStack;

import com.sun.tools.classfile.ConstantPool;

import javax.print.DocFlavor;
import java.util.*;

public class CalculateExpression_after {
    static char[][] pre={
            {'>','>','<','<','<','>','>'},
            {'>','>','<','<','<','>','>'},
            {'>','>','>','>','<','>','>'},
            {'>','>','>','>','<','>','>'},
            {'<','<','<','<','<','=','0'},  // 此行"(" = ")"表示左右括号相遇，括号内运算已完成
            {'>','>','>','>','0','>','>'},
            {'<','<','<','<','<','0','='}   //“#" = "#” 表示整个表达式求值完毕
    };                              //  "0"表示不可能出现这种情况 ( 语法错误 )

    public static char Precede(char a,char b){
        int i=-1;
        int j=-1;
        switch(a){
            case'+':i=0;break;
            case'-':i=1;break;
            case'*':i=2;break;
            case'/':i=3;break;
            case'(':i=4;break;
            case')':i=5;break;
            case'#':i=6;break;   // # 是表达式的结束符
        }
        switch(b){
            case'+':j=0;break;
            case'-':j=1;break;
            case'*':j=2;break;
            case'/':j=3;break;
            case'(':j=4;break;
            case')':j=5;break;
            case'#':j=6;break;
        }
        return pre[i][j];
    }

    public static void Show(Stack s){
        Enumeration elements = s.elements();
        while (elements.hasMoreElements()){
            System.out.print(elements.nextElement());
        }
        System.out.println();
    }

    public static int Operate(int m,int n,char x) {
        if (x == '+')
            return m + n;
        else if (x == '-')
            return m - n;
        else if (x == '*')
            return m * n;
        else if (x == '/')
            return m / n;
        return 0;
    }

    //将读入的中缀表达式转换成后缀表达式
    public static Stack<Object> Translate(){
        Stack<Object> temp=new Stack<Object>();
        Stack<Character> optr=new Stack<Character>();
        optr.push('#');
        Scanner s=new Scanner(System.in);
        String ss=s.next();
        char[] chars = ss.toCharArray();
        int i=0;
        char c=chars[i];
        while (i<= chars.length-1){
            if (Character.isDigit(c)){
                temp.push(c);
                if (i== chars.length-1){
                    break;
                }else {
                    c=chars[++i];
                }
            }else{
                switch (Precede(optr.peek(),c)){
                    case '<':
                        optr.push(c);
                        if (i== chars.length-1){
                            break;
                        }else {
                            c=chars[++i];
                        }
                        break;
                    case '=':
                        optr.pop();
                        if (i== chars.length-1){
                            break;
                        }else {
                            c=chars[++i];
                        }
                        break;
                    case '>':
                        char c1=optr.peek();
                        temp.push(c1);
                        optr.pop();
                        if (i== chars.length-1){
                            break;
                        }else {
                            //c=chars[++i];
                        }
                        break;
                }

            }
        }
        while (optr.peek()!='#'){
            char c2=optr.peek();
            temp.push(c2);
            optr.pop();
        }
        return temp;
    }

    //通过后缀表达式进行计算
    public static void Calculate(Stack stack){
        Stack<Object> temp=new Stack<Object>();
        Enumeration elements = stack.elements();
        while (elements.hasMoreElements()){
            Object o = elements.nextElement();
            if (Character.isDigit(o.toString().toCharArray()[0])){
                temp.push(o);
            }else{
                int b = Integer.parseInt(temp.peek().toString());
                temp.pop();
                int a=Integer.parseInt(temp.peek().toString());
                temp.pop();
                int tempFin = Operate(a, b, o.toString().toCharArray()[0]);
                temp.push(tempFin);
            }
        }
        System.out.println(temp.peek());
    }

    public static void main(String[] args) {
        Stack<Object> translate = Translate();
        CalculateExpression_after.Show(translate);
        CalculateExpression_after.Calculate(translate);
    }

}
